﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DataAccessLayer
{
    public partial class Room
    {
        public static Room FindByRoomID_IncludeDeleted(linqDatabaseDataContext db, int RoomID)
        {
            var subquery = (from u in db.Rooms where u.ID == (from v in db.Rooms where v.RoomID == u.RoomID orderby v.Timestamp descending select v).FirstOrDefault().ID select u);
            return ((from u in subquery where u.RoomID == RoomID select u).SingleOrDefault());
        }

        public static Room FindByRoomID(linqDatabaseDataContext db, int RoomID)
        {
            var subquery = (from u in db.Rooms where u.ID == (from v in db.Rooms where v.RoomID == u.RoomID orderby v.Timestamp descending select v).FirstOrDefault().ID select u);
            return ((from u in subquery where !u.IsDeleted && u.RoomID == RoomID select u).SingleOrDefault());
        }
        
        public static List<Room> ToList_IncludeDeleted(linqDatabaseDataContext db)
        {
            List<Room> list = new List<Room>();
            foreach (Floor floor in Floor.ToList(db))
                list.AddRange(ToListByFloorID_IncludeDeleted(db, floor.ID));
            return list;
        }

        public static List<Room> ToListByFloorID_IncludeDeleted(linqDatabaseDataContext db, int FloorID)
        {
            var subquery = (from u in db.Rooms where u.ID == (from v in db.Rooms where v.RoomID == u.RoomID orderby v.Timestamp descending select v).First().ID select u);
            return ((from u in subquery where u.FloorID == FloorID orderby u.OrderBy ascending select u).ToList());
        }

        public static List<Room> ToList(linqDatabaseDataContext db)
        {
            List<Room> list = new List<Room>();
            foreach (Floor floor in Floor.ToList(db))
                list.AddRange(ToListByFloorID(db, floor.ID));
            return list;
        }

        public static List<Room> ToListByFloorID(linqDatabaseDataContext db, int FloorID)
        {
            var subquery = (from u in db.Rooms where u.ID == (from v in db.Rooms where v.RoomID == u.RoomID orderby v.Timestamp descending select v).First().ID select u);
            return ((from u in subquery where !u.IsDeleted && u.FloorID == FloorID orderby u.OrderBy ascending select u).ToList());
        }

        public static List<Room> HistoryToListByRoomID(linqDatabaseDataContext db, int RoomID)
        {
            return (from u in db.Rooms where u.RoomID == RoomID orderby u.Timestamp ascending select u).ToList();
        }
    }
}
